package com.tanhua.server.controller;

import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsUserVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    /**
     * 根据id获取用户信息
     *
     * @param userID
     * @return
     */
    @GetMapping
    public UserInfoVo findById(Long userID) {
        if (userID == null) {
            Long id = UserHolder.getUserId();
            userID = id;
        }
        UserInfoVo userInfoVo = userInfoService.findById(userID);

        return userInfoVo;
    }

    /**
     * 根据id修改用户详情信息表
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateById(@RequestBody UserInfo userInfo) {
        Long userId = UserHolder.getUserId();

        userInfo.setId(userId);

        userInfoService.updateById(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-发送短信验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String mobile = UserHolder.getMobile();

        userService.sendSms(mobile);

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-校验验证码
     *
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = (String) map.get("verificationCode");

        String mobile = UserHolder.getMobile();

        boolean verification = userService.checkVerificationCode(code, mobile);

        Map map1 = new HashMap();
        map1.put("verification", verification);

        return ResponseEntity.ok(map1);
    }

    /**
     * 修改手机号 - 3 保存
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updateNewPhone(@RequestBody Map map){
        String phone = (String) map.get("phone");

        userService.updateNewPhone(phone);

        return ResponseEntity.ok(null);
    }

    /**
     * 修改头像
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        //https://tanhua001.oss-cn-beijing.aliyuncs.com//2021/05/07/cd212b26-47c8-4058-af77-77e12a549e32.jpg
        userInfoService.updateHeader(headPhoto);

        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        CountsUserVo vo = userService.counts();

        return ResponseEntity.ok(vo);
    }
}
